import Taro from '@tarojs/taro'
import { EventKey } from '../consts'
import dva from '../dva/dva'
import Api from './api/api'
import PayModel from './models/pay_model'

class Global {

    static readonly shared = new Global()

    hasNetwork: boolean
    systemInfo: Taro.getSystemInfoSync.Result
    safeBottom: number

    tabbarHeight: number

    constructor() {
        this.systemInfo = Taro.getSystemInfoSync()
        this.safeBottom = this.systemInfo.screenHeight - this.systemInfo.safeArea.bottom
        this.tabbarHeight = this.safeBottom + 60
    }

    static isLogin(): boolean {
        let app = dva.app
        let user = app.getStore().getState().user
        if (user.isLogin) {
            return true
        }
        return false
    }
    
    static logout() {
        
    }

    static navigationBarHeight() {
        let systemInfo = this.shared.systemInfo
        let statusBarHeight = systemInfo.statusBarHeight
        let contentHeight = systemInfo.system.startsWith("Android") ? 48 : 44
        return statusBarHeight + contentHeight
    }

    static loadImage(imageName?: string, subpackage?: string): any {

        if (!imageName || imageName.length <= 0) {
            return null
        }

        if (imageName.endsWith(".jpg") || imageName.endsWith(".png")) {
            return "$loadImage(`/src/resources/images/${imageName}`)"
        }
    }

    static makeNavigateUrl(url: string, params?: {}) {
        if (params) {
            return url + "?params=" + JSON.stringify(params)    
        }
        return url   
    }

    static navigateTo(url: string, options?: { 
        package?: string, 
        params?: {}, 
        success?: (res: Taro.General.CallbackResult & { eventChannel: Taro.EventChannel }) => void,
        complete?: (res: Taro.General.CallbackResult) => void
    }) {
        let fullUrl
        if (url.startsWith("/")) {
            fullUrl = url
        } else {
            fullUrl = "/" + url
        }
        
        if (options?.package) {
            fullUrl = `/pages/${options.package}${fullUrl}`
        }
        
        if (options?.params) {
            fullUrl = `${fullUrl}?params=${JSON.stringify(options.params)}`
        }

        console.log("navigateTo", fullUrl)
        Taro.navigateTo({
            url: fullUrl,
            success: options?.success,
            complete: options?.complete
        })
    }

    static redirectTo(url: string, options?: { 
        package?: string, 
        params?: {}, 
        success?: (res: Taro.General.CallbackResult & { eventChannel: Taro.EventChannel }) => void,
        complete?: (res: Taro.General.CallbackResult) => void
    }) {

        let fullUrl
        if (url.startsWith("/")) {
            fullUrl = url
        } else {
            fullUrl = "/" + url
        }
        
        if (options?.package) {
            fullUrl = `/pages/${options.package}${fullUrl}`
        }
        
        if (options?.params) {
            fullUrl = `${fullUrl}?params=${JSON.stringify(options.params)}`
        }
        console.log("redirectTo", fullUrl)
        Taro.redirectTo({
            url: fullUrl,
            success: options?.success,
            complete: options?.complete
        })
    }

    static showToast(title: any, icon?: 'success' | 'loading' | 'none') {
        Taro.showToast({title: title, icon: icon ?? "none"})
    }

    static pay(orderId: string, complete: (isOk: boolean) => void) {
        Api.orderPayWx({ orderid: orderId }).then( (res) => {
            if (!Api.handleResponse(res)){
                return
            }
            if (!res) {
                return
            }

            let model: PayModel = res.data
            Taro.requestPayment({
                timeStamp: model.timeStamp,
                nonceStr: model.nonceStr,
                package: model.package,
                signType: model.signType == "MD5" ? "MD5" : "HMAC-SHA256",
                paySign: model.paySign,
                success: () => {
                    complete(true)
                    Taro.eventCenter.trigger(EventKey.OrderPayed, orderId)
                },
                fail: () => {
                    complete(false)
                }
            })
        })
    }

}

export default Global